<?php

namespace app\admin\controller\mall;

use app\common\controller\AdminController;
use app\common\lib\Tree;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="mall_cate")
 */
class Cate extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\MallCate();

        $this->assign('getStatusList', $this->model->getStatusList());

    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $where[] =[['id','not in',[6]]];
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, 1000)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }


    public function add($id = null)
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            if(isset($data['id'])){
                unset($data['id']);
            }
            try {
                $save = $this->model->save($data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $pidMenuList = $this->getTreeCate();
        $this->assign('pidMenuList', array_merge([[
            'id' => 0,
            'pid' => 0,
            'title' => '顶级菜单',
        ]], $pidMenuList));
        $this->assign('id', $id);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if($row['pid']>0){
            $parentinfo =$this->model->where("id",$row['pid'])->find();
            $row['cat_name'] =$parentinfo['title'];
        }else{
            $row['cat_name'] ='请选择';
        }

        $this->assign('row', $row);
        return $this->fetch();
    }


    /**
     * @return array|\int[][]|\string[][]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getTreeCate(): array
    {
        $lists = $this->model->field('id,pid,title')->where('status', 1)
            ->order('sort desc,id desc')
            ->select()
            ->toArray();
        return Tree::getInstance()->getPidMenuList(0, $lists);
    }

    public function getCateByList()
    {
        $treeList = $this->getTreeCate();
        $this->success('ok',$treeList);
    }

    public function getCateByList2()
    {
        $treeList = $this->getTreeCate();
        $treeLists=array();
        foreach ($treeList as $k=>$v){
            $treeLists[$k]['value'] =$v['id'];
            $treeLists[$k]['name'] =$v['title'];
            $treeLists[$k]['selected'] =false;
            $treeLists[$k]['disabled'] =false;
        }

        $this->success('ok',$treeLists);
    }

}